//
//  SWDiscoverSearchView.swift
//  STslender
//
//  Created by mac on 15/12/5.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class SWDiscoverSearchView: UIView ,UITextFieldDelegate
{

    //textField 的右边约束
 
    
    @IBOutlet weak var rightConstraints: NSLayoutConstraint!
    @IBOutlet weak var cancelBtn: UIButton!

    @IBOutlet weak var searchField:
    UITextField!
      //加载 xib
    class func  searchView ()-> SWDiscoverSearchView {
        
        return  NSBundle.mainBundle().loadNibNamed("SWDiscoverSearch", owner: nil, options: nil).last! as! SWDiscoverSearchView
        
    }
   
    
    //    // 与 代码创建控件有关
    //    override init(frame: CGRect) {
    //        super.init(frame: frame)
    //    }

    //必须写init  可以通过代码往当前控件添加子控件
    //    required init?(coder aDecoder: NSCoder) {
    //        super.init(coder: aDecoder)
    //    }
    //
    
     // xib 里面将线与控件连好之后会调用这个方法
    override func awakeFromNib() {
        //设置 textfield的 样式
         searchField.layer.cornerRadius = 5
        //边缘
        searchField.layer.borderColor = UIColor.orangeColor().CGColor
        searchField.layer.borderWidth = 3
        
        
        let leftView =  UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.frame = CGRectMake(0, 0, frame.height, frame.height)
        // 设置内容显示模式
        leftView.contentMode = .Center
        // 设置leftView
        searchField.leftView = leftView
        // 指定其显示模式
        searchField.leftViewMode = UITextFieldViewMode.Always
    
        
    }
    
   
    
    @IBAction func cancelClick(sender: AnyObject) {
        
//        searchField.becomeFirstResponder()
       searchField.resignFirstResponder()
//        self.endEditing(true)
        
        
        self.rightConstraints.constant = 0
    
        UIView.animateWithDuration(0.25) { () -> Void in
            //重新布局
            self.searchField.layoutIfNeeded()
        }
        
        
    }
    
    //textField  的代理方法 UITextFieldDelegate
    
    func textFieldDidBeginEditing(textField: UITextField) {
        
        self.rightConstraints.constant = self.cancelBtn.frame.width
    
        UIView.animateWithDuration(0.25) { () -> Void in
            //重新布局
            self.searchField.layoutIfNeeded()
        }
    }
    
 
    
    
    
}
